package com.yumao.animatordemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button button;
    private A a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
        a = new A(10000, 1000);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageView:
//                Toast.makeText(this, "image click", Toast.LENGTH_SHORT).show();
                final Snackbar snackbar = Snackbar.make(imageView, "Snackbar test", Snackbar.LENGTH_INDEFINITE);
                snackbar.setAction("delete", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        snackbar.dismiss();
                        a.start();
                    }
                });
                snackbar.show();
                break;
            case R.id.button1:
                Intent intent1 = new Intent(this, Main4Activity.class);
                startActivity(intent1);
                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                break;
            case R.id.button2:
                Intent intent2 = new Intent(this, Main2Activity.class);
                startActivity(intent2);
                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                break;
            case R.id.button3:
                Intent intent3 = new Intent(this, Main3Activity.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                break;
            case R.id.button:
//                TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 100);
//                AlphaAnimation animation2 = new AlphaAnimation(1.0f, 0.5f);
//                RotateAnimation animation3 = new RotateAnimation(0f, 360f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//
//                AnimationSet set = new AnimationSet(this, null);
//                set.addAnimation(animation);
//                set.addAnimation(animation2);
//                set.addAnimation(animation3);
//                set.setDuration(1000);
//                set.setFillAfter(true);
//                imageView.startAnimation(set);

//                translationX","translationY","scaleX","scaleY","rotation","alpha" 常用属性
                ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f);
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
                ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
                animator.setDuration(1000);
                animator2.setDuration(1000);
                animator3.setDuration(1000);

                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Toast.makeText(MainActivity.this, "animator end", Toast.LENGTH_SHORT).show();
                    }
                });
//                animator.start();//该方法异步执行
//                animator2.start();

                AnimatorSet set = new AnimatorSet();
                set.play(animator).with(animator2).before(animator3);
//                set.playTogether(animator, animator2, animator3);
                set.start();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (a != null) {
            a.cancel();
        }
    }

    private class A extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public A(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            String a = String.valueOf(millisUntilFinished / 1000);
            CharSequence sequence = TextUtils.concat("还有", a, "秒");
            button.setText(sequence);
        }

        @Override
        public void onFinish() {
            button.setText("move");
        }
    }
}
